import { SpriteFrame } from "cc";

const INDEX_REG = /\((\d+)\)/;

const getNumberWithinString = (str: string) => parseInt(str.match(INDEX_REG)?.[1] || "0");

export const sortSpriteFrame = (spriteFrame: Array<SpriteFrame>) =>
    spriteFrame.sort((a, b) => getNumberWithinString(a.name) - getNumberWithinString(b.name));


//弧度转角度
export const rad2Angle = (rad: number) => {
    return rad * 180 / Math.PI;
}
/**
 * 深克隆
 * @param obj 
 * @returns 
 */
export const deepClone = (obj:any)=>{
    //如果参数类型是基本类型，返回obj
    if (typeof obj !== "object" || obj === null) {
        return obj;
    }
    //判断是不是数组，是则创建一个空数组，否则返回空对象
    const res = Array.isArray(obj) ? [] : {};
        for (const key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
            //递归调用深克隆res保存返回值
            res[key] = deepClone(obj[key]);
        }
    }
    return res
}